/**
  ******************************************************************************
  * @file           : studio_udp.h
  * @author         : wangyingjie
  * @brief          : socket upd 类封装 基于 boost::asio
  * @attention      : None
  * @date           : 2025/7/2
  ******************************************************************************
  */

#ifndef STUDIO_UDP_H
#define STUDIO_UDP_H

#include "studio_macros.h"

#include <boost/asio.hpp>

namespace asio = boost::asio;
using asio::ip::udp;

class studio_udp
{
public:
    using receive_callback = std::function<void(const std::string&, const udp::endpoint&)>;

    /// 构造函数
    /// @param local_ip 本地ip
    /// @param local_port 本地端口
    /// @param remote_ip 远程ip
    /// @param remote_port 远程端口
    /// @param buffer_size 缓冲区大小 默认4096 字节 4KB
    studio_udp(
        const std::string& local_ip,
        unsigned short local_port,
        const std::string& remote_ip,
        unsigned short remote_port,
        size_t buffer_size = 4096);

    ~studio_udp();

    // 启动服务
    bool start();

    // 停止服务
    void stop();

    // 发送数据
    void send(const std::string& message);

    // 设置接收数据回调
    void set_receive_callback(receive_callback callback);

private:
    void do_receive();
    void do_heartbeat();

    asio::io_context io_context_;
    udp::socket socket_;
    udp::endpoint remote_endpoint_;
    std::atomic<bool> running_;
    size_t buffer_size_;

    std::thread io_thread_;

    receive_callback recv_callback_;
    std::string heartbeat_message_;
};

#endif //STUDIO_UDP_H